package com.example.designpattern.prototype;

public class PrototypePatternDemo {
    public static void main(String[] args) {

    }
    public static class Component {
        private String name;
        public Component(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        @Override
        public String toString() {
            return "Component{" +
                    "name='" + name + '\'' +
                    '}';
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return new Component(getName());
        }
    }
    public static class Product {
        private String name;
        private Component component;

        public Product(String name, Component component) {
            this.name = name;
            this.component = component;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Component getComponent() {
            return component;
        }

        public void setComponent(Component component) {
            this.component = component;
        }

        @Override
        public String toString() {
            return "Product{" +
                    "name='" + name + '\'' +
                    ", component=" + component +
                    '}';
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {
            // 浅拷贝，就是我们现在的一个实现
            // 就是仅仅简单的对当前所有的变量进行一个拷贝
            //return new Product(getName(), getComponent());

            // 深考别，递归对自己引用的对象也进行拷贝
            return new Product(getName(),(Component) getComponent().clone());
        }
    }
}
